//
//  TopMenuView.swift
//  QQ空间
//
//  Created by 叶竞泽 on 2017/5/9.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit

protocol TopMenuViewDelegate: NSObjectProtocol {
    func didClickMenuItem(fromButton withTag: UIButton)
}

class TopMenuView: UIView {
    
    // MARK: - 属性
    
    /// 父视图
    var inView: UIView
    /// 中间视图
    var centerView: UIView
    /// 底部视图(下方的视图)
    var bottomView: UIView
    /// infoButton
    var infoButton: UIButton?
    /// nameLabel
    var nameLabel: UILabel?
    /// 代理
    weak var delegate: TopMenuViewDelegate?
    
    /// 不同设备方向时视图的高度
    private var heightForPortrait: CGFloat {
        return inView.bounds.height * percentHeightInPortrait
    }
    private var heightForLandscape: CGFloat {
        return inView.bounds.height * percentHeightInLandscape
    }
    
    /// 宽度在父视图的比例(竖屏)
    private var percentHeightInPortrait: CGFloat = 0.5
    /// 宽度在父视图的宽度(横屏)
    private var percentHeightInLandscape: CGFloat = 0.6
    
    // MARK: - 生命周期
    
    override func layoutSubviews() {
        super.layoutSubviews()
        layoutButton()
    }

    /// 初始化
    ///
    /// - Parameters:
    ///   - inView: 父视图
    ///   - bottomView: 下方视图
    init(inView: UIView, centerView: UIView?, bottomView: UIView?) {
        self.inView = inView
        self.centerView = centerView!
        self.bottomView = bottomView!
        super.init(frame: CGRect.zero)
        // 根据设备方向设置frame
        changeOrientation()
        // 创建子控件按钮
        createSubButton()
        // 创建名字label
        createTitleLabel()
        // 监听屏幕旋转
        NotificationCenter.default.addObserver(self, selector: #selector(changeOrientation), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 方法
    
    /// 监听设备旋转
    @objc private func changeOrientation() {
        switch UIDevice.current.orientation {
        case .portrait: fallthrough
        case .portraitUpsideDown:
            frame = CGRect(x: 0, y: 0, width: inView.bounds.width, height: inView.bounds.height - bottomView.bounds.height - centerView.bounds.height)
        case .landscapeLeft: fallthrough
        case .landscapeRight:
            frame = CGRect(x: 0, y: 0, width: inView.bounds.width, height: inView.bounds.height - bottomView.bounds.height - centerView.bounds.height)
        default:
            break
        }
    }
    
    @objc fileprivate func infoButtonClick(sender: UIButton) {
        delegate?.didClickMenuItem(fromButton: sender)
    }
}

// MARK: - 设置界面
extension TopMenuView {
    
    /// 创建infoButton
    fileprivate func createSubButton() {
        let button = UIButton(type: .custom)
        button.frame = bounds
        button.setImage(UIImage(named: "Easy"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        button.addTarget(self, action: #selector(infoButtonClick(sender:)), for: .touchUpInside)
        infoButton = button
        addSubview(button)
    }
    fileprivate func createTitleLabel() {
        let titleLabel = UILabel()
        titleLabel.text = "shanshanlol"
        titleLabel.textColor = UIColor.white
        titleLabel.isHidden = true
        nameLabel = titleLabel
        titleLabel.sizeToFit()
        addSubview(titleLabel)
    }
    
    /// 根据设备方向布局infoButton
    fileprivate func layoutButton() {
        guard let button = infoButton else { return }
        switch UIDevice.current.orientation {
        case .portrait: fallthrough
        case .portraitUpsideDown:
            button.frame = CGRect(x: 0, y: frame.height * 0.5, width: inView.bounds.width, height: inView.bounds.width)
            button.setTitle("", for: .normal)
            button.imageView?.layer.cornerRadius = (button.imageView?.bounds.width)! / 2
            button.imageView?.layer.masksToBounds = true
            button.contentEdgeInsets = UIEdgeInsetsMake(2, 2, 2, 2)
            nameLabel?.isHidden = true
        case .landscapeLeft: fallthrough
        case .landscapeRight:
            button.frame = CGRect(x: 0, y: 0, width: inView.bounds.width, height: inView.bounds.height - bottomView.bounds.height - centerView.bounds.height)
            button.setTitle("", for: .normal)
            button.imageView?.layer.cornerRadius = (button.imageView?.bounds.width)! / 2
            button.imageView?.layer.masksToBounds = true
            nameLabel?.frame = CGRect(x: button.center.x - (nameLabel?.frame.width ?? 0) / 2, y: (button.imageView?.frame.maxY ?? 0) + 10, width: 0, height: 0)
            nameLabel?.sizeToFit()
            nameLabel?.isHidden = false
        default:
            break
        }
    }
    

}
